﻿var ChartMuaChung = {
    _topChart: 9,
    _leftChart: 0,
    _sel_divPoint: "#divPoint",
    _sel_currentPoint: ".chart_muachung_yellow",

    _getNumber: function (num) {
        var k = 0;
        if (num < 0) num = -num;
        while (num != 0) {
            num = parseInt(num / 10);
            k += 1;
        }
        return k;
    },
    drawPoint: function (elementID, data) {
        if (data == null) {
            return "";
        }
        var _max = data[0][data[0].length - 1];
        var _width = $(elementID).width();
        var _top = $(elementID).position().top;
        var _left = $(elementID).position().left;
        var _length = data[0].length;
        var _k = (_width - 16) / ((_max - 1) * 1.0); // hệ số nhân của vị trí đặt điểm giá
        var _s1 = '';
        var _s2 = '';
        var _s3 = '';
        for (var i = 0; i < _length; i++) {
            if ((i > 0) && (i < _length - 1)) {
                _s1 += '<img src="/plugin/chartMuachung/images/chart_muachung_point.png" class="chart_muachung_point" style="top:'
                            + (1 + _top) + 'px;left:'
                            + (8 + _left + parseInt((data[0][i] - 1) * _k)) + 'px;" alt=""/>';
            }
            if (i < _length - 1) {
                _s2 += '<span class="chart_muachung_point" style="top:'
                        + (-10 + _top) + 'px; left:'
                        + (8 + _left + parseInt((data[0][i] - 1) * _k) - ChartMuaChung._getNumber(data[0][i]) * 2) + 'px">'
                        + data[0][i] + '</span>';
            }
            else {
                _s2 += '<span class="chart_muachung_point" style="top:'
                        + (-10 + _top) + 'px; left:'
                        + (8 + _left + parseInt((data[0][i] - 1) * _k) - ChartMuaChung._getNumber(data[0][i]) * 2) + 'px">'
                        + data[0][i];
                if ((typeof (data[3]) != 'undefined') && (data[0][i] != data[3])) {
                    _s2 += '-<span style="font-weight:bold;">' + data[3] + '</span>';
                }

                _s2 += '</span>';
            }
            _s3 += '<span class="chart_muachung_point" style="top:'
                        + (17 + _top) + 'px; left:'
                        + (8 + _left + parseInt((data[0][i] - 1) * _k) - ChartMuaChung._getNumber(data[1][i]) * 2) + 'px">'
                        + data[1][i] + '</span>';
        }
        $(ChartMuaChung._sel_divPoint, elementID).html(_s1 + _s2 + _s3);
        if (data[2][0] > 0) {
            if (data[2][0] < data[0][_length - 1]) {
                $(ChartMuaChung._sel_currentPoint, elementID).width((parseInt(data[2][0]) - 1) * _k + 9);
            }
            else {
                $(ChartMuaChung._sel_currentPoint, elementID).width(_width);
            }
        }
    },
    bindChart: function (elementID, data) {
        var _w = 196;
        if ((typeof (data[3]) != 'undefined') && (data[3] != data[0][data[0].length - 1])) {
            _w -= ChartMuaChung._getNumber(data[3]) * 5;
        }

        var s = '<div id="divMuaChung" class="chart_muachung_blue" style="width:' + _w + 'px;">'
                + '<div class="chart_muachung_yellow">'
                + '<div class="chart_muachung" style="width:' + _w + 'px">'
                + '<div class="chart_muachung_left"></div>'
                + '<div class="chart_muachung_right"></div></div></div><div id="divPoint" class="clear"></div></div><div></div>';
        $("#" + elementID).html(s);
        ChartMuaChung.drawPoint("#divMuaChung", data);
    }
};